package ui;

import controller.ProductController;
import project_package.Product;

import java.awt.event.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.UnsupportedLookAndFeelException;
import java.awt.Component;

public class ProductUI implements ActionListener, ListSelectionListener
{
	
	private ProductController productCtrl;	
	private JFrame frame;
	private JPanel addProduct, editProduct;
	private JTabbedPane tabbedPane;
	private static ProductUI instance = null;
	private JButton addProductButtonReset;	
	private JButton addProductButtonAdd;
	private JButton addProductButtonClose;
	private JButton addProductButtonExit;
	private JTextField addProductTextFieldName;
	private JTextField addProductTextFieldPrice;
	private JTextField addProductTextFieldDescription;
	private JTextField addProductTextFieldQuantity;
	private JCheckBox addProductCheckBoxAalborg;
	private JCheckBox addProductCheckBoxOdense;
	private JCheckBox addProductCheckBoxCopenhagen;
	private JComboBox<Double> addProductComboBoxDiscount;
	
	private JScrollPane editProductSP;
	private JList<String> editProductList;
	private DefaultListModel<String> listModel;
	private JButton editProductButtonClose;
	private JButton editProductButtonExit;
	private JButton editProductButtonDelete;
	private JButton editProductButtonReset;
	private JButton editProductButtonUpdate;
	private JCheckBox editProductCheckBoxAalborg;
	private JCheckBox editProductCheckBoxOdense;
	private JCheckBox editProductCheckBoxCopenhagen;
	private JTextField editProductTextFieldName;
	private JTextField editProductTextFieldPrice;
	private JTextField editProductTextFieldQuantity;
	private JTextField editProductTextFieldDescription;
	private JTextField editProductTextFieldPriceDiscount;
	private JComboBox<Double> editProductComboBoxDiscount;
	

	public static ProductUI getInstance()
    {
    	if(instance == null)
    	{
    		instance = new ProductUI();
    	}
    	return instance;
    }
	
	public void display()
	{
		frame.getContentPane().add(tabbedPane);
		tabbedPane.add("Add product", addProduct);
		tabbedPane.add("Edit product", editProduct);
		frame.setSize(510,450);
		frame.setVisible(true);
		frame.setResizable(false);
	}
	
	public ProductUI()
	{
		
		frame = new JFrame("Product UI");
		tabbedPane = new JTabbedPane();
		productCtrl = new ProductController();

		fakeProductGenerator();

		//**********************************
		// PAGE 1
		//**********************************
		
		addProduct = new JPanel();
		
		JLabel addProductLabelName = new JLabel("Product Name");
		JLabel addProductLabelPrice = new JLabel("Product Price");		
		JLabel addProductLabelDescription = new JLabel("Product Description");		
		JLabel addProductLabelDiscount = new JLabel("Product Discount");		
		JLabel addProductLabelQuantity = new JLabel("Product Quantity");		
		JLabel addProductLabelLocation = new JLabel("Product Location");
		addProductCheckBoxAalborg = new JCheckBox("Aalborg");		
		addProductCheckBoxOdense = new JCheckBox("Odense");		
		addProductCheckBoxCopenhagen = new JCheckBox("Copenhagen");	
		addProductComboBoxDiscount = new JComboBox<Double>();

		addProductButtonReset = new JButton("Reset");	
		addProductButtonReset.addActionListener(this);		
		addProductButtonAdd = new JButton("Add");
		addProductButtonAdd.addActionListener(this);
		addProductButtonClose = new JButton("Close window");
		addProductButtonClose.addActionListener(this);
		addProductButtonExit = new JButton("Exit");
		addProductButtonExit.addActionListener(this);
		
		for(double i = 0; i <= 100; i++) {
			addProductComboBoxDiscount.addItem(i);
		}
		
		addProductTextFieldName = new JTextField(30);
		addProductTextFieldPrice = new JTextField(6);
		addProductTextFieldDescription = new JTextField(32);
		addProductTextFieldQuantity = new JTextField(10);
		
		GroupLayout gl_addProduct = new GroupLayout(addProduct);
		gl_addProduct.setHorizontalGroup(
			gl_addProduct.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_addProduct.createSequentialGroup()
					.addGap(31)
					.addGroup(gl_addProduct.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_addProduct.createSequentialGroup()
							.addGroup(gl_addProduct.createParallelGroup(Alignment.LEADING)
								.addComponent(addProductLabelName)
								.addComponent(addProductLabelPrice)
								.addComponent(addProductLabelDescription)
								.addComponent(addProductLabelDiscount)
								.addComponent(addProductLabelQuantity)
								.addComponent(addProductLabelLocation))
							.addGap(82))
						.addGroup(gl_addProduct.createSequentialGroup()
							.addGap(34)
							.addGroup(gl_addProduct.createParallelGroup(Alignment.TRAILING)
								.addComponent(addProductButtonAdd, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)
								.addComponent(addProductButtonClose, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE))
							.addGap(40)))
					.addGroup(gl_addProduct.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_addProduct.createParallelGroup(Alignment.LEADING, false)
							.addComponent(addProductCheckBoxOdense)
							.addComponent(addProductCheckBoxAalborg)
							.addComponent(addProductTextFieldName, GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE)
							.addComponent(addProductTextFieldPrice)
							.addComponent(addProductTextFieldDescription)
							.addComponent(addProductTextFieldQuantity)
							.addGroup(gl_addProduct.createSequentialGroup()
								.addPreferredGap(ComponentPlacement.RELATED)
								.addGroup(gl_addProduct.createParallelGroup(Alignment.TRAILING, false)
									.addComponent(addProductButtonExit, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
									.addComponent(addProductCheckBoxCopenhagen, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
									.addComponent(addProductButtonReset, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 96, GroupLayout.PREFERRED_SIZE))
								.addGap(25)))
						.addComponent(addProductComboBoxDiscount, GroupLayout.PREFERRED_SIZE, 83, GroupLayout.PREFERRED_SIZE))
					.addGap(31))
		);
		gl_addProduct.setVerticalGroup(
			gl_addProduct.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_addProduct.createSequentialGroup()
					.addGap(58)
					.addGroup(gl_addProduct.createParallelGroup(Alignment.BASELINE)
						.addComponent(addProductLabelName)
						.addComponent(addProductTextFieldName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_addProduct.createParallelGroup(Alignment.BASELINE)
						.addComponent(addProductLabelPrice)
						.addComponent(addProductTextFieldPrice, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_addProduct.createParallelGroup(Alignment.BASELINE)
						.addComponent(addProductLabelDescription)
						.addComponent(addProductTextFieldDescription, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_addProduct.createParallelGroup(Alignment.BASELINE)
						.addComponent(addProductLabelDiscount)
						.addComponent(addProductComboBoxDiscount, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_addProduct.createParallelGroup(Alignment.BASELINE)
						.addComponent(addProductLabelQuantity)
						.addComponent(addProductTextFieldQuantity, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_addProduct.createParallelGroup(Alignment.BASELINE)
						.addComponent(addProductLabelLocation)
						.addComponent(addProductCheckBoxAalborg))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_addProduct.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_addProduct.createSequentialGroup()
							.addComponent(addProductCheckBoxOdense)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(addProductCheckBoxCopenhagen)
							.addGap(7)
							.addComponent(addProductButtonReset))
						.addComponent(addProductButtonAdd))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_addProduct.createParallelGroup(Alignment.BASELINE)
						.addComponent(addProductButtonClose)
						.addComponent(addProductButtonExit))
					.addContainerGap(57, Short.MAX_VALUE))
		);
		addProduct.setLayout(gl_addProduct);

		//**********************************
		// PAGE 2
		//**********************************
		
		editProduct = new JPanel();
		
		listModel = new DefaultListModel<String>();
		editProductSP = new JScrollPane();

		updateProductList();
		
		editProductTextFieldName = new JTextField(32);
		editProductTextFieldName.setEditable(false);
		editProductTextFieldPrice = new JTextField(10);
		editProductTextFieldQuantity = new JTextField(10);
		editProductTextFieldDescription = new JTextField(32);
		editProductTextFieldPriceDiscount = new JTextField(10);
		editProductTextFieldPriceDiscount.setEditable(false);
		
		JLabel editProductLabelName = new JLabel("Product Name");
		JLabel editProductLabelPrice = new JLabel("Product Price");
		JLabel editProductLabelDiscount = new JLabel("Product Discount");
		JLabel editProductLabelQuantity = new JLabel("Product Quantity");
		JLabel editProductLabelDescription = new JLabel("Product Description");
		JLabel editProductLabelLocation = new JLabel("Product Location");
		JLabel editProductLabelPriceDiscount = new JLabel("Product Current Price");
		
		editProductComboBoxDiscount = new JComboBox<Double>();
		
		for(double i = 0; i <= 100; i++) {
			editProductComboBoxDiscount.addItem(i);
		}
		
		editProductButtonClose = new JButton("Close window");
		editProductButtonClose.addActionListener(this);
		editProductButtonExit = new JButton("Exit");
		editProductButtonExit.addActionListener(this);
		editProductButtonDelete = new JButton("Delete");
		editProductButtonDelete.setEnabled(false);
		editProductButtonDelete.addActionListener(this);
		editProductButtonReset = new JButton("Reset");
		editProductButtonReset.setEnabled(false);
		editProductButtonReset.addActionListener(this);
		editProductButtonUpdate = new JButton("Update");
		editProductButtonUpdate.setEnabled(false);
		editProductButtonUpdate.addActionListener(this);
	
		editProductCheckBoxAalborg = new JCheckBox("Aalborg");
		editProductCheckBoxOdense = new JCheckBox("Odense");
		editProductCheckBoxCopenhagen = new JCheckBox("Copenhagen");
				
		GroupLayout gl_editProduct = new GroupLayout(editProduct);
		gl_editProduct.setHorizontalGroup(
			gl_editProduct.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_editProduct.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_editProduct.createParallelGroup(Alignment.LEADING)
						.addComponent(editProductLabelDescription)
						.addComponent(editProductLabelLocation)
						.addComponent(editProductLabelName)
						.addComponent(editProductLabelDiscount)
						.addComponent(editProductButtonDelete)
						.addComponent(editProductLabelPrice)
						.addComponent(editProductLabelPriceDiscount)
						.addComponent(editProductLabelQuantity))
					.addGap(58)
					.addGroup(gl_editProduct.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_editProduct.createSequentialGroup()
							.addComponent(editProductComboBoxDiscount, GroupLayout.PREFERRED_SIZE, 54, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED))
						.addGroup(gl_editProduct.createParallelGroup(Alignment.TRAILING)
							.addGroup(gl_editProduct.createSequentialGroup()
								.addGroup(gl_editProduct.createParallelGroup(Alignment.TRAILING)
									.addComponent(editProductTextFieldDescription, GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE)
									.addComponent(editProductCheckBoxAalborg, Alignment.LEADING)
									.addComponent(editProductCheckBoxOdense, Alignment.LEADING)
									.addComponent(editProductCheckBoxCopenhagen, Alignment.LEADING)
									.addComponent(editProductTextFieldQuantity, GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE))
								.addPreferredGap(ComponentPlacement.RELATED))
							.addGroup(gl_editProduct.createSequentialGroup()
								.addGap(10)
								.addComponent(editProductButtonUpdate)
								.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(editProductButtonReset)
								.addGap(18))
							.addGroup(gl_editProduct.createSequentialGroup()
								.addGroup(gl_editProduct.createParallelGroup(Alignment.LEADING)
									.addGroup(gl_editProduct.createSequentialGroup()
										.addPreferredGap(ComponentPlacement.RELATED)
										.addComponent(editProductTextFieldPriceDiscount, GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE))
									.addComponent(editProductTextFieldName, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE)
									.addComponent(editProductTextFieldPrice, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE))
								.addPreferredGap(ComponentPlacement.RELATED))))
					.addGroup(gl_editProduct.createParallelGroup(Alignment.LEADING, false)
						.addComponent(editProductButtonExit, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(editProductButtonClose, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(editProductSP, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE))
					.addContainerGap())
		);
		gl_editProduct.setVerticalGroup(
			gl_editProduct.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_editProduct.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_editProduct.createParallelGroup(Alignment.TRAILING, false)
						.addGroup(gl_editProduct.createSequentialGroup()
							.addGroup(gl_editProduct.createParallelGroup(Alignment.BASELINE)
								.addComponent(editProductLabelName)
								.addComponent(editProductTextFieldName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(gl_editProduct.createParallelGroup(Alignment.BASELINE)
								.addComponent(editProductLabelPrice)
								.addComponent(editProductTextFieldPrice, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(gl_editProduct.createParallelGroup(Alignment.BASELINE)
								.addComponent(editProductLabelDiscount)
								.addComponent(editProductComboBoxDiscount, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(gl_editProduct.createParallelGroup(Alignment.BASELINE)
								.addComponent(editProductTextFieldPriceDiscount, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)
								.addComponent(editProductLabelPriceDiscount))
							.addGap(15)
							.addGroup(gl_editProduct.createParallelGroup(Alignment.BASELINE)
								.addComponent(editProductTextFieldQuantity, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(editProductLabelQuantity))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(gl_editProduct.createParallelGroup(Alignment.LEADING)
								.addComponent(editProductLabelDescription)
								.addComponent(editProductTextFieldDescription, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addGap(13)
							.addGroup(gl_editProduct.createParallelGroup(Alignment.BASELINE)
								.addComponent(editProductLabelLocation)
								.addComponent(editProductCheckBoxAalborg))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(editProductCheckBoxOdense)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(editProductCheckBoxCopenhagen))
						.addComponent(editProductSP, GroupLayout.PREFERRED_SIZE, 306, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(editProductButtonClose)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_editProduct.createParallelGroup(Alignment.BASELINE)
						.addComponent(editProductButtonExit)
						.addComponent(editProductButtonDelete)
						.addComponent(editProductButtonReset)
						.addComponent(editProductButtonUpdate))
					.addContainerGap(14, Short.MAX_VALUE))
		);
		gl_editProduct.linkSize(SwingConstants.VERTICAL, new Component[] {editProductTextFieldName, editProductTextFieldPrice, editProductTextFieldDescription, editProductLabelName, editProductLabelPrice, editProductLabelDiscount, editProductLabelQuantity, editProductLabelDescription});
		
		editProductList = new JList<String>(listModel);
		editProductSP.setViewportView(editProductList);
		editProductList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		editProductList.setSelectedIndex(0);
		editProductList.setVisibleRowCount(4);
		editProductList.addListSelectionListener(this);
		editProduct.setLayout(gl_editProduct);
	}
	
	public boolean contains(String haystack, String needle) 
	{
		  haystack = haystack == null ? "" : haystack;
		  needle = needle == null ? "" : needle;

		  return haystack.toLowerCase().contains(needle.toLowerCase());
	}
	
	public void fakeProductGenerator() 
	{
		productCtrl.createProduct("Orange", 15.5, 0.05, "Orange fruit", 1, "Aalborg");
		productCtrl.createProduct("Kiwi", 32.4, 0.2, "Superb", 5, "Copenhagen");
		productCtrl.createProduct("Apple", 4.5, 0.0, "Beautiful", 10, "Aalborg Odense");
		productCtrl.createProduct("Blackberry", 0.9, 0.2, "Rageveul", 24, "Aalborg Copenhagen");
		productCtrl.createProduct("Strawberry", 100.4, 0.01, "Bravo", 32, "Odense");
	}
	
	public void updateProductList()
	{
		listModel.clear();
		HashMap<Integer, Product> products = productCtrl.getProducts();
    	Set<Map.Entry<Integer, Product>> st = products.entrySet();
    	for(Map.Entry<Integer,Product> product : st)
    	{
    		listModel.addElement(product.getValue().getName());
		}
	}
	
	public void productShow(String productName)
	{
		Product product = productCtrl.findProduct(productName);
		editProductTextFieldName.setText(productName);
		editProductTextFieldPrice.setText(Double.toString(product.getPrice()));
		editProductComboBoxDiscount.setSelectedItem(product.getDiscount()*100);
		editProductTextFieldQuantity.setText(Integer.toString(productCtrl.getQuantity(product)));
		editProductTextFieldPriceDiscount.setText(Double.toString(product.getPriceDiscount()));
		editProductTextFieldDescription.setText(product.getDescription());
		
		if (contains(productCtrl.getLocation(product), editProductCheckBoxAalborg.getText()))
			editProductCheckBoxAalborg.setSelected(true);
		else
			editProductCheckBoxAalborg.setSelected(false);
		
		if (contains(productCtrl.getLocation(product), editProductCheckBoxOdense.getText()))
			editProductCheckBoxOdense.setSelected(true);
		else
			editProductCheckBoxOdense.setSelected(false);

		if (contains(productCtrl.getLocation(product), editProductCheckBoxCopenhagen.getText()))
			editProductCheckBoxCopenhagen.setSelected(true);
		else
			editProductCheckBoxCopenhagen.setSelected(false);

	}
	
	public void resetEditInfo() 
	{
		editProductTextFieldName.setText("");
		editProductTextFieldPrice.setText("");
		editProductTextFieldQuantity.setText("");
		editProductTextFieldDescription.setText("");
		editProductTextFieldPriceDiscount.setText("");
		editProductComboBoxDiscount.setSelectedIndex(0);
	}
	
	public boolean isDouble(String number) 
	{
		try {
			Double.parseDouble(number);
			return true;
		}
		catch(Exception e) {
			return false;
		}
	}
	
	public boolean isNumber(String number) 
	{
		try {
			Integer.valueOf(number);
			return true;
		}
		catch(Exception e) {
			return false;
		}
	}
	
	@SuppressWarnings({ "deprecation", "unchecked" })
	public void valueChanged(ListSelectionEvent e) {
	   	if (e.getSource() == editProductList)
	   	{
	   		boolean adjust = e.getValueIsAdjusting();
	        if (!adjust)
	        {
	    		editProductButtonReset.setEnabled(true);
	    		editProductButtonDelete.setEnabled(true);
	    		editProductButtonUpdate.setEnabled(true);
	        	JList<String> productList = (JList<String>) e.getSource();
	        	int selections[] = productList.getSelectedIndices();
	        	Object selectionValues[] = productList.getSelectedValues();
	        	for (int i = 0; i < selections.length; i++) {
	        		productShow(selectionValues[i].toString());
	        	}
	        }
	   	}
	}
	
	 // Buttons
	public void actionPerformed(ActionEvent e) 
	{
		
		// Close window
        if((e.getSource() == editProductButtonClose) || (e.getSource() == addProductButtonClose)) 
        	frame.dispose();
        
        // Exit
        else if((e.getSource() == editProductButtonExit) || (e.getSource() == addProductButtonExit)) 
        	System.exit(0);
      
        // Add product - Add
        else if (e.getSource() == addProductButtonAdd) {
        	if (!addProductCheckBoxAalborg.isSelected() && !addProductCheckBoxOdense.isSelected() && !addProductCheckBoxCopenhagen.isSelected())
			{
				JOptionPane.showMessageDialog(addProduct, "You should select at least one shop","Error", 0, null);
			} 
        	else if (addProductTextFieldPrice.getText().isEmpty()
					|| addProductTextFieldQuantity.getText().isEmpty()
					|| addProductTextFieldName.getText().isEmpty()
					|| addProductTextFieldDescription.getText().isEmpty()) 
        	{
				JOptionPane.showMessageDialog(addProduct, "You have left empty fields","Error", 0, null);					
			} 
			else if (!isDouble(addProductTextFieldPrice.getText()) || (isDouble(addProductTextFieldPrice.getText()) && Double.parseDouble(addProductTextFieldPrice.getText()) < 1))
				JOptionPane.showMessageDialog(addProduct, "Price should be a number over zero","Error", 0, null);									
			else if (!isNumber(addProductTextFieldQuantity.getText()) || (isNumber(addProductTextFieldQuantity.getText()) && Integer.parseInt(addProductTextFieldQuantity.getText()) < 1))
				JOptionPane.showMessageDialog(addProduct, "Quantity should be an integer over zero","Error", 0, null);									
			else {
				double productPrice = Double.parseDouble(addProductTextFieldPrice.getText());
				double productDiscount = (double) addProductComboBoxDiscount.getSelectedItem();
				productDiscount = productDiscount/100;
				int productQuantity = Integer.parseInt(addProductTextFieldQuantity.getText());
				String productName = addProductTextFieldName.getText();
				String productDescription = addProductTextFieldDescription.getText();
				String productLocation = (addProductCheckBoxAalborg.isSelected() ? "Aalborg " : "") + (addProductCheckBoxOdense.isSelected() ? "Odense " : "") + (addProductCheckBoxCopenhagen.isSelected() ? "Copenhagen" : "");
				boolean success = productCtrl.createProduct(productName, productPrice, productDiscount, productDescription, productQuantity, productLocation);
				if (success)
					JOptionPane.showConfirmDialog(addProduct, "Successfully added product " + productName, "Create", JOptionPane.PLAIN_MESSAGE, JOptionPane.INFORMATION_MESSAGE);
				else
					JOptionPane.showConfirmDialog(addProduct, "There was error adding the product", "Create", JOptionPane.PLAIN_MESSAGE, JOptionPane.WARNING_MESSAGE);						
				updateProductList();
			}
        }
        
        // Add product - Reset
        else if (e.getSource() == addProductButtonReset) 
        {
        	addProductTextFieldName.setText("");
        	addProductTextFieldPrice.setText("");
        	addProductTextFieldDescription.setText("");
        	addProductTextFieldQuantity.setText("");
        	addProductComboBoxDiscount.setSelectedIndex(0);
        	addProductCheckBoxAalborg.setSelected(false);
        	addProductCheckBoxOdense.setSelected(false);
        	addProductCheckBoxCopenhagen.setSelected(false);
        }
        
        // Edit product - Delete
        else if(e.getSource() == editProductButtonDelete)
        {
        	if (!editProductTextFieldName.getText().isEmpty())
			{
				int response = JOptionPane.showConfirmDialog(null, "Are you sure you want to delete " + editProductTextFieldName.getText() + "?", "Delete", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
				if (response == JOptionPane.YES_OPTION) {
					boolean success = productCtrl.deleteProduct(editProductTextFieldName.getText());
					
					if (success)
						JOptionPane.showConfirmDialog(editProduct, "Successfully deleted product " + editProductTextFieldName.getText(), "Delete", JOptionPane.PLAIN_MESSAGE, JOptionPane.INFORMATION_MESSAGE);
					else
						JOptionPane.showConfirmDialog(editProduct, "There was error deleting the product", "Delete", JOptionPane.PLAIN_MESSAGE, JOptionPane.WARNING_MESSAGE);						
				}
			}
			updateProductList();
			resetEditInfo();
        }
        
        // Edit product - Reset
        else if (e.getSource() == editProductButtonReset) 
        {
        		productShow(editProductTextFieldName.getText());
        }
        
        // Edit product - Update
        else if (e.getSource() == editProductButtonUpdate)
        {
        	if (!editProductCheckBoxAalborg.isSelected() && !editProductCheckBoxOdense.isSelected() && !editProductCheckBoxCopenhagen.isSelected())
			{
				JOptionPane.showMessageDialog(editProduct, "You should select at least one shop","Error", 0, null);
			} else if (editProductTextFieldPrice.getText().isEmpty()
					|| editProductTextFieldQuantity.getText().isEmpty()
					|| editProductTextFieldDescription.getText().isEmpty()) 
			{
				JOptionPane.showMessageDialog(editProduct, "You have left empty fields","Error", 0, null);					
			}
			else if (!isDouble(editProductTextFieldPrice.getText()) || (isDouble(editProductTextFieldPrice.getText()) && Double.parseDouble(editProductTextFieldPrice.getText()) < 1))
				JOptionPane.showMessageDialog(editProduct, "Price should be a number over zero","Error", 0, null);									
			else if (!isNumber(editProductTextFieldQuantity.getText()) || (isNumber(editProductTextFieldQuantity.getText()) && Integer.parseInt(editProductTextFieldQuantity.getText()) < 0))
				JOptionPane.showMessageDialog(editProduct, "Quantity should be an integer over or equal zero","Error", 0, null);									
			else {
				
				double productPrice = Double.parseDouble(editProductTextFieldPrice.getText());
				double productDiscount = (double) editProductComboBoxDiscount.getSelectedItem();
				productDiscount = productDiscount/100;
				int productQuantity = Integer.parseInt(editProductTextFieldQuantity.getText());
				String productName = editProductTextFieldName.getText();
				String productDescription = editProductTextFieldDescription.getText();
				String productLocation = (editProductCheckBoxAalborg.isSelected() ? "Aalborg " : "") + (editProductCheckBoxOdense.isSelected() ? "Odense " : "") + (editProductCheckBoxCopenhagen.isSelected() ? "Copenhagen" : "");
				boolean success = productCtrl.updateProduct(productCtrl.findProduct(productName), productName, productPrice, productDiscount, productDescription, productQuantity, productLocation);
				updateProductList();
				if (success) 
				{
					JOptionPane.showConfirmDialog(editProduct, "Successfully updated product " + productName, "Update", JOptionPane.PLAIN_MESSAGE, JOptionPane.INFORMATION_MESSAGE);
					productShow(productName);
				}
				else {
					JOptionPane.showConfirmDialog(editProduct, "There was error updating the product", "Update", JOptionPane.PLAIN_MESSAGE, JOptionPane.WARNING_MESSAGE);						
				}
			}
        }
    }  
	
	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException 
	{
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		ProductUI productMenu = new ProductUI();
		productMenu.display();
	}
}
